home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PUBLISHING / Tex-Edit Plus 2.1.sit / Tex-Edit Plus 2.1 / WTHI AppleScript? < prev    next >
Text File  |  1997-07-31  |  15KB  |  388 lines

  1.  
  2.                What the Heck is AppleScript?
  3.  
  4. AppleScript is an English-like programming language that allows you to use Apple Events to control applications.
  5.  
  6. Great.  Whatユs an Apple Event?
  7.  
  8. With the advent of System 7, Apple introduced a powerful new way for applications to communicate with each other.  Each Apple Event represents a single メmessageモ in this digital dialog.
  9.  
  10. For instance, when you double-clicked this document, the Finder actually sent an メopen documentモ Apple Event to Tex-Edit.  Tex-Edit then recognized the event and displayed this document for you.
  11.  
  12. Whatユs the big deal?
  13.  
  14. Well, Apple Events allow メscriptableモ applications to communicate at a very intimate level.  Unlike macros, Apple Events bypass the user interface and are quite efficient.
  15.  
  16. AppleScript puts the power of these Apple Events in the hands of the ordinary user.  Itユs just another insanely great advantage that we Mac users enjoy.
  17.  
  18. What does it mean to say an application is メscriptable?モ
  19.  
  20. Scriptability implies that the programmer has given AppleScript access to major portions of the applicationユs inner workings.
  21.  
  22. If AppleScript is so great, why arenユt all applications scriptable?
  23.  
  24. Changing an existing application to make it scriptable involves an effort that I like to refer to as メnon-trivial.モ Luckily, most significant applications are now scriptable.
  25.  
  26. Do I have to learn to program to use AppleScript?
  27.  
  28. Well, it kind of depends on what you mean by メprogramming.モ The AppleScript dialect is very forgiving and looks a lot like ordinary English.
  29.  
  30. No you donユt understand.  I REALLY hate programming!
  31.  
  32. Youユre in luck!
  33.  
  34. Tex-Edit, like many other scriptable applications, is also メrecordable.モ Appleユs Script Editor can record your actions as you use Tex-Edit.  The resulting メscriptモ (program) can be saved and re-played later.
  35.  
  36. So, AppleScript isnユt just for nerds?
  37.  
  38. The real nerds are the folks who waste time doing repetitive computing chores, instead of letting their Mac do the work.
  39.  
  40. Okay, show me an example of an AppleScript program.
  41.  
  42. Well, letユs see.  How about if we create a メstyle sheetモ program which sets the font of the text to 12 point Monaco (ick!) and then sets the size of the first character of every paragraph to 24 points.
  43.  
  44. Just how much programming work would that require?
  45.  
  46. You just saw it.
  47.  
  48. Huh?
  49.  
  50. Well, actually the program might look something like this:
  51.  
  52.    tell application "Tex-Edit Plus"
  53.       set the font of the text of the first window to "Monaco"
  54.       set the size of the text of the first window to 12
  55.       set the size of the first character of every paragraph of window 1 to 24
  56.    end tell
  57.  
  58. Not bad! What does the first line do?
  59.  
  60. Itユs just a shortcut.  Notice how the メtellモ and メend tellモ lines bracket all the commands.  This saves us a little typing, otherwise the program would read:
  61.  
  62.    set the font of the text of the first window
  63.       of application "Tex-Edit Plus" to "Monaco"
  64.    set the size of the text of the first window
  65.       of application "Tex-Edit Plus" to 12
  66.    set the size of the first character of every paragraph of window 1
  67.       of application "Tex-Edit Plus" to 24
  68.  
  69. But, doesnユt every word have to be in just the right place?
  70.  
  71. AppleScript is a very forgiving programming language.  For example, these two programs do the same thing:
  72.  
  73.    set the size of the first word to ten
  74.    set size of word 1 to 10
  75.  
  76. Okay already! How do I add scripts to Tex-Editユs □ menu?
  77.  
  78. When Script Editor saves a script as a メcompiled script,モ it creates a script document which can be placed in Tex-Editユs メScriptsモ folder.  When Tex-Edit launches, it adds everything in this folder to its Scripts menu, providing easy access to your favorite scripts.
  79.  
  80. I donユt see a □ menu.
  81.  
  82. Either there is no メScriptsモ folder in the same folder as Tex-Edit or you have a System version earlier than 7.5.
  83.  
  84. Well, I have System 7.5, but I meticulously removed lots of stuff in the Extensions folder to make my system more stable.
  85.  
  86. Donユt do that!  You probably managed to remove lots of optimized code patches and bug fixes.  Itユs not worth the tiny amount of disk space you saved.
  87.  
  88. I want to install AppleScript.  Where can I find it?
  89.  
  90. AppleScript and Script Editor are included free with all Systems since 7.5.  If you have an earlier version of System 7, you can download the latest AppleScript installer from Appleユs web site:
  91. <http://applescript.apple.com/>
  92.  
  93. How can I learn more about writing scripts?
  94.  
  95. AppleScript is a rich, full-featured programming language with variables, subroutines, loops and branches.  If you want to appreciate the full power of AppleScript, I would recommend Danny Goodmanユs メAppleScript Handbook.モ Itユs an excellent source of information, especially for beginners.
  96.  
  97. You should also subscribe to the MACSCRPT mailing list.  It is very active and includes lots of helpful information from lots of AppleScript users.  To subscribe to MACSCRPT:
  98. send email to: <mailto:LISTSERV@dartmouth.edu>
  99. with subject: メsubscribeモ
  100. and message: メsubscribe macscrpt <your full name here>モ.
  101.  
  102. Finally, just experiment with Script Editorユs メrecordモ function to see examples of the syntax Tex-Edit uses when sending messages to itself.
  103.  
  104. But, I want to write a script NOW!
  105.  
  106. Okay, first launch Tex-Edit and then launch Script Editor (which should be located in the Apple menu).  To execute an AppleScript command, simply type the command into the Script Editor window, then press the メrunモ button:
  107.  
  108.      □
  109.  
  110. In an AppleScript program, each line is a separate command.  Usually the commands are bracketed by メtellモ and メend tellモ statements, as explained above.  When the program runs, command lines are executed sequentially from top to bottom.
  111.  
  112. Try typing in and running the following program.  (Lines which start with a double hyphen are optional comments.) 
  113.  
  114.  
  115. Once you get a program to do what you want it to do, save it as a メcompiled scriptモ document inside of Tex-Editユs メScriptsモ folder.
  116.  
  117. What commands does Tex-Edit understand?
  118.  
  119. Every scriptable application contains a メdictionaryモ which can be explored using Script Editor.  I have included a brief synopsis of Tex-Editユs dictionary below, but I would recommend using Script Editor to print out the real thing.
  120.  
  121. How do I interpret whatユs in the dictionary?
  122.  
  123. The dictionary is divided up into chapters (or メsuitesモ)--Required Suite, Standard Suite, Text Suite, etc.  Each suite heading is followed by a list of commands and objects (or メclassesモ).
  124.  
  125. The dictionary defines every AppleScript word that is recognized by Tex-Edit, but it does not explain all the different possible usages.  Try using Script Editorユs メrecordモ button to see examples of acceptable syntax.
  126.  
  127. Whatユs an object?
  128.  
  129. In the program:
  130.  
  131.    close window 1 of application "Tex-Edit Plus"
  132.  
  133. the verb メcloseモ is the command and メwindow 1 of application Tex-Edit Plusモ is the object.
  134.  
  135. Many of the objects have メproperties.モ You can use the Get and Set commands (described below) to extract an objectユs property and change it to something different.  Notice how the メofモ keyword can chain together a list of objects, allowing you to unambiguously specify any given object.
  136.  
  137.    tell application "Tex-Edit Plus"
  138.       get name of window 1
  139.       set size of character 1 of word 1 of window "untitled" to 24
  140.    end tell
  141.  
  142. In the first command, メnameモ is a property of the object メwindow 1 of application Tex-Edit Plus.モ  In the second command, メsizeモ is a property of the object メcharacter 1 of word 1 of window "untitled" of application Tex-Edit Plus.モ
  143.  
  144. _____________________________
  145.  
  146.  
  147. Commonly-used commands:
  148.  
  149. Here are some of the basic commands understood by most applications along with some simple examples of usage.  Notice that many commands correspond to standard menu items.  Examine each example to see the basic syntax.  Of course, the best way to understand the commands is to actually type them in and try them out.
  150.  
  151. activate
  152.    Brings an application to the front.
  153.    ex:
  154.       activate application "Tex-Edit Plus"
  155.  
  156. quit
  157.    Quits an application.
  158.    ex:
  159.       quit application "Tex-Edit Plus"
  160.       quit application "Tex-Edit Plus" saving no
  161.  
  162. open
  163.    Opens a document file.
  164.    ex:
  165.       tell application "Tex-Edit Plus"
  166.          open file "MacHD:Saving the World"
  167.       end tell
  168.  
  169. print
  170.    Prints a document or window.
  171.    ex:
  172.       print every window of application "Tex-Edit Plus"
  173.       print the last window of application "Tex-Edit Plus"
  174.  
  175. close
  176.    Closes the given window.
  177.    ex:
  178.       close every window of application "Tex-Edit Plus"
  179.       close windows 2 through 4 of application "Tex-Edit Plus"
  180.  
  181. count
  182.    Returns the number of items.
  183.    ex:
  184.       count the words of window 2 of application "Tex-Edit Plus"
  185.       count the windows of application "Tex-Edit Plus"
  186.  
  187. delete
  188.    Deletes the given text.
  189.    ex:
  190.       tell window 1 of application "Tex-Edit Plus"
  191.          delete the first word
  192.          delete every character whose color is red
  193.       end tell
  194.  
  195. get
  196.    Gets whatever information you ask for.  Get and Set are used a lot!
  197.    ex:
  198.       tell application "Tex-Edit Plus"
  199.          get the font of the third word of window 1
  200.          get the position of the first window
  201.       end tell
  202.  
  203. set
  204.    Changes the value of an objectユs property.
  205.    ex:
  206.       tell window 1 of application "Tex-Edit Plus"
  207.          set the color of the last word to red
  208.          set the name to "Fun With AppleScript"
  209.       end tell
  210.  
  211. make
  212.    Creates a new window or text item.
  213.    ex:
  214.       tell application "Tex-Edit Plus"
  215.          make new window behind window 1
  216.          make new line at end of text of window 1 with data "Dear Occupant:"
  217.       end tell
  218.  
  219. save
  220.    Saves the window to disk.
  221.    ex:
  222.       tell application "Tex-Edit Plus"
  223.          save the first window in "MacHD:Letter to MicroFlaccid"
  224.       end tell
  225.  
  226. select
  227.    Selects the desired text or window.
  228.    ex:
  229.       tell application "Tex-Edit Plus"
  230.          select the last document of application "Tex-Edit Plus"
  231.          select text from character 1 to character 60000 of window 2
  232.       end tell
  233.  
  234. undo/copy/cut/paste/revert
  235.    Executes the familiar menu command.
  236.    ex:
  237.       tell application "Tex-Edit Plus"
  238.          copy
  239.          copy unstyled
  240.       end tell
  241.  
  242.  
  243. Other commands (specific to Tex-Edit):
  244.  
  245. add line endings
  246.    Adds CRs and LFs to the text.
  247.    ex:
  248.       tell selection of application "Tex-Edit Plus"
  249.          add line endings with crlf
  250.       end tell
  251.  
  252. block format
  253.    Converts the given text into an indented block.
  254.    ex:
  255.       tell the first window of application "Tex-Edit Plus"
  256.          block format the first paragraph with line header ">  "
  257.          block format the last paragraph with line length 45
  258.       end tell
  259.  
  260. change case
  261.    Changes the case of the given text.
  262.    ex:
  263.       tell the middle window of application "Tex-Edit Plus"
  264.          change case of line 3 to uppercase
  265.       end tell
  266.  
  267. search
  268.    Searches for the given text.
  269.    ex:
  270.       search the last window of application "Tex-Edit Plus" for "important item"
  271.  
  272. replace
  273.    Replaces all occurrences of one string with another.
  274.    ex:
  275.       tell application "Tex-Edit Plus"
  276.          replace window 1 from "Copland" to "Allegro"
  277.          replace the last window from multiple spaces to tab
  278.       end tell
  279.  
  280. smarten/stupefy
  281.    Transforms to/from typographical characters.
  282.    ex:
  283.       smarten the first window of application "Tex-Edit Plus" for quotes
  284.  
  285. speak
  286.    Read the given text aloud.
  287.    ex:
  288.       speak "hello"
  289.       speak first paragraph of the third window of application "Tex-Edit Plus"
  290.  
  291. strip
  292.    Remove the specified characters from the text.
  293.    ex:
  294.       tell application "Tex-Edit Plus"
  295.          strip the first window for leading spaces
  296.       end tell
  297.  
  298. strip line endings
  299.    Remove commonly used line ending characters.
  300.    ex:
  301.       tell window 2 of application "Tex-Edit Plus"
  302.          strip line endings with cr
  303.          strip line endings of selection with crlf
  304.       end tell
  305.  
  306.  
  307. Objects:
  308.  
  309. Many objects can be combined with the preceding commands into meaningful statements, however some combinations are not allowed.
  310.  
  311. For example you can:
  312.    close window 1 of application "Tex-Edit Plus"
  313.    print window 1 of application "Tex-Edit Plus"
  314.  
  315. but you canユt:
  316.    delete window 1 of application "Tex-Edit Plus"
  317.    quit window 1 of application "Tex-Edit Plus"
  318.  
  319. メTrial and errorモ is a good way to get started.  It is also helpful to use Script Editorユs メrecordモ function to watch what happens as you use Tex-Edit.  Note the use of the Get and Set commands to access the properties of each object.  Here are a few of the available objects along with a few of their properties:
  320.  
  321. application
  322.    This is Tex-Edit itself.  Its properties are メglobal.モ
  323.    properties:
  324.       selection:  The current text selection in the front window.
  325.       search string:  The string used in the Find dialog.
  326.       speech on:  Used to turn on/off the Speech Manager functions.
  327.    ex:
  328.       tell application "Tex-Edit Plus"
  329.          get selection
  330.          set font of selection to "Monaco"
  331.          set search string to "interesting stuff"
  332.          set speech on to true
  333.       end tell
  334.  
  335. window
  336.    This is synonymous with メdocument.モ
  337.    properties:
  338.       name:  The title of the document.
  339.       position:  The coordinates of the top left corner.
  340.       justification:  Used to set text justification in the window.
  341.    ex:
  342.       tell application "Tex-Edit Plus"
  343.          get name of window 1
  344.          set the position of some window to {125, 250}
  345.          set justification of every document to center
  346.       end tell
  347.  
  348. character
  349.    This is a single character in a window.
  350.       font:  The font face of the character.
  351.       size:  Font size of the character.
  352.       style:  Styles of the character (bold, italic, etc.).
  353.       color:  Color of the character.
  354.    ex:
  355.       tell window 3 of application "Tex-Edit Plus"
  356.          get the font of character 1
  357.          set size of (text from character 1 to word 10) to 24
  358.          set the style of character 1 to {bold, italic, underline}
  359.          set color of the last character to red
  360.       end tell
  361.  
  362. word-line-paragraph-text
  363.    These objects share all the properties of character.
  364.    ex:
  365.       tell window 1 of application "Tex-Edit Plus"
  366.          get the font of word 1
  367.          set the color of paragraph 2 to blue
  368.          set size of line 1 to 24
  369.          set style of (text from word 2 to word 4) to bold
  370.       end tell
  371.  
  372. _____________________________
  373.  
  374.  
  375. AppleScript seems so simple.  Are there any limits?
  376.  
  377. This document barely brushes the surface of the possibilities.  My favorite trick is to use voice-actuated メstyle sheetモ scripts to switch styles as I type.
  378.  
  379. The sample scripts in Tex-Editユs メScriptsモ folder will help you get started.  Feel free to examine them and change them as much as you wish.
  380.  
  381. Where can I find more sample scripts?
  382.  
  383. There are hundreds of useful scripts located all over the web, available to anyone with a modem and a phone jack.  Also, be sure to check out the numerous AppleScript extensions (OSAX) that add even more power.
  384.  
  385. So, what are you waiting for?  You have nothing to lose but your tired fingers.
  386.  
  387. (Document Revised 6/28/97)